package com.hand.springcloud.filter;


import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @ Description   :  自定义登录过滤器
 * @ Author        :  xiong.yilin@hand-china.com
 * @ CreateDate    :  2019/9/10 11:46
 */
@SuppressWarnings("ALL")
@Component //一定不要少了
public class LoginFilter extends ZuulFilter {

    Logger logger = LoggerFactory.getLogger(getClass());

    // 请求路由前调用
    @Override
    public String filterType() {
        return "pre";
    }

    // int值来定义过滤器的执行顺序，数值越小优先级越高
    @Override
    public int filterOrder() {
        return 1;
    }

    // 该过滤器是否执行，true执行，false不执行
    @SuppressWarnings("AlibabaCommentsMustBeJavadocFormat")
    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        RequestContext context = RequestContext.getCurrentContext();
        HttpServletRequest request = context.getRequest();
        // 获取请求参数的token值
        String token = request.getParameter("token");
        if (token == null) {
            logger.warn("此操作需要先登录系统");
            context.setSendZuulResponse(false);  // 拒绝访问
            context.setResponseStatusCode(200);  // 设置响应状态码
            try {
                // 响应结果
                context.getResponse().getWriter().write("token is empty");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
        logger.info("ok");
        return null;
    }
}
